package org.example.hypersonaltools.tool;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;

public class FileTools {
    public List<String> getChapterMessage(File selectedFile) {
        List<String> chapters = new ArrayList<>();
        try {
            List<String> lines = Files.readAllLines(selectedFile.toPath());
            StringBuilder currentChapter = new StringBuilder();
            for (String line : lines) {
                if (line.trim().matches("第[零一二三四五六七八九十百千万]+章\\s+.*")){
                    if (currentChapter.length() > 0) {
                        chapters.add(currentChapter.toString());
                        currentChapter.setLength(0); // 清空当前章节内容
                    }
                    currentChapter.append(line).append("\n"); // 添加章节标题
                }else {
                    currentChapter.append(line);
                }
            }
            if (currentChapter.length() > 0) {
                chapters.add(currentChapter.toString()); // 添加最后一个章节
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return chapters;
    }
}
